﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PickGold.Callers
{
	class CRemoteProcedureCallAsyncResult : IAsyncResult
	{
		internal ManualResetEvent InnerWaitHandle;
		internal Thread InnerThread;
		internal object InnerAsyncState;
		internal bool InnerCompleted;

		public object Result;
		public AsyncCallback Callback;
		public DateTime StartTime;
		public Exception Error;

		public CRemoteProcedureCallAsyncResult(object @asyncState)
		{
			this.StartTime = DateTime.Now;
			this.InnerThread = Thread.CurrentThread;
			this.InnerWaitHandle = new ManualResetEvent(false);//new AutoResetEvent(true);//
			this.InnerAsyncState = @asyncState;
		}

		public CRemoteProcedureCallAsyncResult(AsyncCallback callback, object @asyncState)
		{
			this.StartTime = DateTime.Now;
			this.InnerThread = Thread.CurrentThread;
			this.InnerWaitHandle = new ManualResetEvent(false);//new AutoResetEvent(true);//
			this.InnerAsyncState = @asyncState;
			this.Callback = callback;
		}

		/// <summary>
		/// 获取用户定义的对象，它限定或包含关于异步操作的信息。
		/// </summary>
		public object AsyncState
		{
			get
			{
				return this.InnerAsyncState;
			}
		}

		/// <summary>
		/// 获取用于等待异步操作完成的 System.Threading.WaitHandle。
		/// </summary>
		public WaitHandle AsyncWaitHandle
		{
			get
			{
				return this.InnerWaitHandle;
				//throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 获取异步操作是否同步完成的指示。
		/// </summary>
		public bool CompletedSynchronously
		{
			get
			{
				if (this.InnerThread == Thread.CurrentThread)
					return true;

				return false;
			}
		}

		/// <summary>
		/// 获取异步操作是否已完成的指示。
		/// </summary>
		public bool IsCompleted
		{
			get
			{
				return this.InnerCompleted;
			}
		}
	}
}
